//
//  HZEmoticonKetboardView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/18.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit

class HZEmoticonKetboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - 设置视图
    func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        // 添加控件
        addSubview(emoticonBottomView)
        addSubview(emoticonPageView)
        // 添加约束
        emoticonBottomView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        emoticonPageView.snp_makeConstraints { (make) in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp_top)
        }
        
        
        // 底部视图闭包回调
        emoticonBottomView.closure = {(type: HZEmoticonBottomViewType)->() in
            switch type {
            case .Default:
                print("默认")
            case .Emoji:
                print("emoji")
            case .Lxh:
                print("浪小花")
            }
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            
            self.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
    }

    //MARK:- 懒加载控件
    // 底部视图
    private lazy var emoticonBottomView: HZEmoticonBottomView = HZEmoticonBottomView(frame: CGRectZero)
    // 自定义表情键盘
    private lazy var emoticonPageView: HZEmoticonPageView = {
        let view = HZEmoticonPageView()
        // 分页
        view.pagingEnabled = true
        // 取消弹簧
        view.bounces = false
        // 隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        // 设置代理
        view.delegate = self
        return view
    }()
}

extension HZEmoticonKetboardView : UICollectionViewDelegate {
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 获取屏幕中心点x( 相对HZEmoticonPageView )
        let centerx = SCREENW/2 + scrollView.contentOffset.x
        // 获取屏幕显示的cell                             由于系统没有帮我们排序 需要我们自己排序
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x > cell2.frame.origin.x
        }
        // 如果屏幕上显示两个cell  判断
        if cells.count == 2 {
            // 第一个cell
            let firstCell = cells.first!
            // 最后一个cell
            let lastCell = cells.last!
            // 定义一个indexPath
            let indexPath: NSIndexPath
            // 判断中心是否在firstCell的frame中
            if firstCell.frame.contains(CGPoint(x: centerx, y: 0)) {
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
            }else{
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
            }
            // 设置选中的按钮
            emoticonBottomView.setupCurrentButton(indexPath.section)
        }
        
    }
    
}








